package whf.framework.meta;

import java.util.List;

import whf.framework.cache.oscache.MetaOSCache;
import whf.framework.dao.DAO;
import whf.framework.entity.Entity;
import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.meta.service.ObjectService;
import whf.framework.meta.service.ObjectServiceImp;
import whf.framework.service.Service;
import whf.framework.util.BeanFactory;
import whf.framework.web.struts.DispatchAction;

/**
 * 业务对象类型标识
 * 
 * @author wanghaifeng
 * 
 */
public class MetaFactory {
	
	private static MetaOSCache metaCache = new MetaOSCache();

	private static boolean initialized = false;

	private static Log log = LogFactory.getLog(MetaFactory.class);
	
	static {
		initialize();
	}
	
	/**
	 * 初始化元数据库
	 */
	synchronized public static void initialize() {
		if (initialized) return;
		try{
			IMetaParser metaParser = new XmlMetaParser();
			List<Meta> xmlMetas = metaParser.listMetas();
			for(Meta meta: xmlMetas){
				metaCache.putMeta(meta);
			}
			ObjectService objectService = ObjectServiceImp.getObjectService();
			List<whf.framework.meta.entity.Object> metas = objectService.findAllWithoutLazy();
			for(Object bo: metas){
				whf.framework.meta.entity.Object meta = (whf.framework.meta.entity.Object)bo;
				metaCache.putMeta(meta.toMeta());
			}
			initialized = true;
		}catch(Exception e){
			log.error(MetaFactory.class, e);
		}
	}

	public static void addMeta(Meta meta) throws MetaValidationException{
		if(meta == null) return;
		if(BeanFactory.inWebModule()){
			meta.validate();
		}
		metaCache.putMeta(meta);
	}

	/**
	 * 通过类名搜索业务对象元数据
	 * @modify wanghaifeng Oct 28, 2006 10:00:33 PM
	 * @param modelClass
	 * @return
	 */
	public static Meta findByModelClass(Class<? extends Entity> modelClass) {
		return metaCache.getMeta(modelClass.getName());
	}

	/**
	 * @modify wanghaifeng Oct 28, 2006 10:00:29 PM
	 * @param actionClass
	 * @return
	 */
	public static Meta findByActionClass(Class<? extends DispatchAction> actionClass) {
		return metaCache.getMeta(actionClass.getName());
	}

	/**
	 * 通过服务类查找
	 * 
	 * @param serviceClass
	 * @return 服务对应的元数据
	 */
	public static Meta findByServiceClass(Class<? extends Service> serviceClass) {
		Meta meta = metaCache.getMeta(serviceClass.getName());
		if(meta == null){
			meta = metaCache.getMeta(serviceClass.getName()+"Imp");
		}
		return meta;
	}

	/**
	 * 通过DAO类查找
	 * 
	 * @param daoClass
	 * @return DAO类对应元数据
	 */
	public static Meta findByDAOClass(Class<? extends DAO> daoClass) {
		Meta meta = metaCache.getMeta(daoClass.getName());
		if(meta == null){
			meta = metaCache.getMeta(daoClass.getName()+"Imp");
		}
		return meta;
	}
	
	public static Meta findByServiceName(String serviceName) {
		return metaCache.getMeta(serviceName);
	}

	public static void main(String[] args) throws Exception{
		List<Meta> metas = metaCache.getMetas();
		for(Meta meta : metas){
			System.out.println(meta);
		}
	}
}
